package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConScene;
import com.htgd.gardenexpo.page.SceneQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeBaseUserService;
import com.htgd.gardenexpo.service.GeConSceneService;
import com.htgd.gardenexpo.service.WxUserCollectService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import com.htgd.gardenexpo.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 景区百科表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-08
 */
@RestController
@RequestMapping("/api/scene")
@Api(tags = "景区百科")
@Slf4j
public class GeConSceneController {

    @Autowired
    private GeConSceneService geConSceneService;

    @Autowired
    private GeBaseUserService geBaseUserService;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Autowired
    private WxUserCollectService wxUserCollectService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add (@RequestBody GeConScene geConScene){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConSceneService.save(geConScene);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit (@RequestBody GeConScene geConScene){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            if(geConScene.getEditorId() != null){
                geConScene.setEditorName(geBaseUserService.queryById(geConScene.getEditorId()).getUsername());
            }
            geConSceneService.updateById(geConScene);
            result.setCode(200);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete (int id){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConSceneService.removeById(id);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConScene>> list(@RequestBody SceneQuery sceneQuery,HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("景点-查询列表；{},{}",sceneQuery,request);
        CommonResult<List<GeConScene>> result = new CommonResult<>();
        try {
            PageInfo<GeConScene> list=geConSceneService.queryList(sceneQuery,openId);
            result.setCode(200);
            result.setData(list.getList());
            result.setMessage(String.valueOf(list.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult<JSONObject> review(GeConScene geConScene) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try{
            if (geConScene.getPublisherId()!=null){
                geConScene.setPublisherName(geBaseUserService.queryById(geConScene.getPublisherId()).getUsername());
                geConScene.setReleasetime(new Date());
            }
            if (geConScene.getAuthUserId()!=null){
                geConScene.setAuthUser(geBaseUserService.queryById(geConScene.getAuthUserId()).getUsername());
                geConScene.setAuthtime(new Date());
            }
            geConSceneService.updateById(geConScene);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/details")
    @ApiOperation("详情")
    @SecurityParameter
    public synchronized CommonResult<GeConScene> details(@RequestBody String s, HttpServletRequest request){
        CommonResult<GeConScene> result = new CommonResult<>();
        try{
            Integer id= JSON.parseObject(s).getInteger("id");
            //点击加1
            GeConScene geConScene = geConSceneService.getById(id);
            geConScene.setHit(geConScene.getHit()+1);
            geConSceneService.updateById(geConScene);
            String accessToken = request.getHeader("Access_token");
            String openId = TokenUtil.getUsernameFromToken(accessToken);
            Boolean collect = wxUserCollectService.isLike(openId, geConScene.getId().toString(),"scene");
            if (collect) {
                geConScene.setIsLike(true);
            }else{
                geConScene.setIsLike(false);
            }
            geConScene.setRecommendTypeText(geBaseDictService.getRecommendTypeText(geConScene.getRecommendType()));
            if(Objects.equals(geConScene.getHot(),"0")) geConScene.setHotText("普通");
            if(Objects.equals(geConScene.getHot(),"1")) geConScene.setHotText("热门");
            result.setData(geConScene);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }
}
